package org.nokatag.Connection;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;

import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
import org.nokatag.system.Concest;


/**
 * Proxool连接池
 * @author rebin
 *
 */
public class ProxoolConnection implements DBConnectionInterface {
	private static Boolean IS_INIT_CONFIG=false;//是否已经初始化配置
	
	private void ProxoolConnectionInit(){
		try{
			if(!IS_INIT_CONFIG){
				File f=new File(getClass().getResource("../../../proxool.xml").toURI());
				InputStream in = new FileInputStream(f);
				Reader reader = new InputStreamReader(in);
				JAXPConfigurator.configure(reader, false);
				IS_INIT_CONFIG=true;
				in.close();
			}
		}catch(Exception se){
			se.printStackTrace();
		}
	}
	/**
	 * 获取connection
	 */
	public Connection getConnection() {
		ProxoolConnectionInit();
		try{
			return DriverManager.getConnection("proxool."+Concest.PROXOOL_ALIAS);
		}catch(Exception se){
			se.printStackTrace();
		}
		return null;
	}

	/**
	 * 关闭连接
	 */
	public void closeConnection(Connection con) {
		try{
			if(null!=con)
				con.close();
		}catch(Exception se){
		}
	}

}
